//index.js
//获取应用实例
var app = getApp();
var util = require('../../utils/util.js');
Page({
    data: {
        tabbar: {},
        teamPartnerStatus: "",
        empClass: "",
        token: "",
        addtell: {
            addtellHidden: true, //弹出框显示/隐藏
        },
        incomeData: {},
        imageList: [
            {
                url: '../../images/index/indexShow.png'
            },
            {
                url: '../../images/index/indexShow1.png'
            },
            {
                url: '../../images/index/indexShow2.png'
            }

        ],

        indicatorDots: true, // 是否显示面板指示点

        autoplay: true, //是否自动切换

        circular: true, //是否采用衔接滑动

        vertical: false, //滑动方向是否为纵向

        interval: 3000, //自动切换时间间隔

        duration: 1000, //滑动动画时长
    },
    // 审批中
    newsFun: function (event) {
        wx.setStorageSync("orderStatus", event.currentTarget.dataset.bookId)
    },
    //   已放款
    referen: function (event) {
        wx.setStorageSync("orderStatus", event.currentTarget.dataset.bookId)
    },
    // 中止
    suspend: function (event) {
        wx.setStorageSync("orderStatus", event.currentTarget.dataset.bookId)
    },
    // 我要录单
    news: function (event) {
        wx.setStorageSync('productId', event.currentTarget.dataset.id)
        wx.navigateTo({
            url: '/pages/order/order',
        })
    },
    details: function (e) {
        console.log(e)
        wx.navigateTo({
            url: '/pages/account/setting',
            success: function (res) {
                let data = e.currentTarget.dataset.id
                res.eventChannel.emit('acceptDataFromOpenerPage', data)
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // util.isLoginModal();
        app.editTabbar();
        wx.hideTabBar()
        var that = this;
        that.setData({
            teamPartnerStatus: wx.getStorageSync("teamPartnerStatus"),
            empClass: wx.getStorageSync('empClass'),
            token: wx.getStorageSync('token')
        })

        //接口API授权 type 1.是公共授权  2.登录授权
        if (!wx.getStorageSync("userid")) {
            util.authorization(1, function (data) {
                //微信授权登录
                util.wxLogin();
                //首页统计货量
                that.getIndexData();
            });
        } else if (wx.getStorageSync("userid")) {
            util.authorization(2, function (data) {
                //首页统计货量
                that.getIndexData();
            });
        }

    },
    // 分享
    onShareAppMessage: function (res) {
        var that = this;
        return {
            title: '贷投狗小程序',
            path: 'pages/index/index',
            success: function (res) {
                that.shareClick();
            },
            fail: function (res) {
            }
        }
    },
    college: function (e) {
        console.log(e)
        let id = e.currentTarget.dataset.id
        if (id == 1) {
            wx.navigateTo({
                url: '/pages/account/organizingdata',
                success: function (res) {
                    let data = id
                    res.eventChannel.emit('acceptDataFromOpenerPage', data)
                }
            })

        } else if (id == 7) {
            wx.navigateTo({
                url: '/pages/account/account',
                success: function (res) {
                    let data = id
                    res.eventChannel.emit('acceptDataFromOpenerPage', data)
                }
            })
        } else {
            wx.showToast({
                title: '暂未开通',
                icon: 'none'
            })
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onLaunch: function () {
        wx.hideTabBar()
    },
    onPullDownRefresh() {
        wx.showNavigationBarLoading();
        this.getIndexData();
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        wx.hideTabBar({
            fail: function () {
                setTimeout(function () {
                    wx.hideTabBar()
                })
            }
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 获取首页数据
     */
    getIndexData: function () {
        var that = this;
        if (!that.data.token) {
            return false
        }
        //首页订单显示统计
        util.https(app.globalData.api + "/applet/home/census", "GET", {},
            function (data) {
                if (data.code == 200) {
                    that.setData({
                        censusData: data.data
                    })
                } else {
                    wx.navigateTo({
                        url: 'pages/account/login',
                    })
                }
            }
        )
        // 总收入
        util.https(app.globalData.api + "/applet/home/income", "GET", {},
            function (data) {
                if (data.code == 200) {
                    data.data.totalMoney = util.formatDecimal(data.data.totalMoney, 2)
                    that.setData({
                        incomeData: data.data
                    })
                } else {
                    wx.navigateTo({
                        url: 'pages/account/login',
                    })
                }
            }
        )
        // 产品数据
        util.https(app.globalData.api + "/applet/home/projectMode", "GET", {},
            function (data) {
                if (data.code == 200) {
                    data.data.map(item => {
                        item.projectModes.map(i => {
                            i.yearRate = util.formatDecimal(i.yearRate, 3)
                            return i
                        })
                        return item
                    })
                    that.setData({
                        projectModeData: data.data
                    })
                    wx.setStorageSync('projectModeLength', that.data.projectModeData.length)
                } else {
                    wx.navigateTo({
                        url: 'pages/account/login',
                    })
                }
            }
        )
        // 主栏目数据
        util.https(app.globalData.api + "/applet/home/mainColumn", "GET", {},
            function (data) {
                if (data.code == 200) {
                    that.setData({
                        mainColumnData: data.data
                    })
                } else {
                    wx.navigateTo({
                        url: 'pages/account/login',
                    })
                }
            }
        )
    },
    footAddtell: function () {
        //打开弹出框
        this.setData({
            addtell: {
                addtellHidden: false,
                contract_info: ''
            }
        })
    },
    modalConfirm: function () {
        //弹出框确认操作
        this.setData({
            addtell: {
                addtellHidden: true,
            }
        })
    },
    modalCancel: function () {
        //弹出框取消操作
        this.setData({
            addtell: {
                addtellHidden: true,
            }
        })
    },
    saveUsertell: function (e) {
        //保存input框的值
        this.setData({
            contract_info: e.detail.value,
            addtell: {
                addtellHidden: false,
            }
        })
    }
})
